<template>
	<view class="uni-combox">
		<view v-if="label" class="uni-combox__label" :style="labelStyle">
			<text>{{label}}</text>
		</view>
		<view class="uni-combox__input-box">
			<input class="uni-combox__input" type="text" ref="inputs" :placeholder="placeholder" v-model="inputVal" @input="onInput"
			 @focus="onFocus" @blur="onBlur" />
			<!-- <uni-icons class="uni-combox__input-arrow" type="arrowdown" size="14" @click="toggleSelector"></uni-icons> -->
			<view class="uni-combox-mask" v-if="showSelector" @tap="onMaskTap"></view>
			<view class="uni-combox__selector" v-if="showSelector">
				<!-- <view class="uni-combox-mask" @tap="onMaskTap"></view> -->
				<scroll-view scroll-y="true" class="uni-combox__selector-scroll">
					<view class="uni-combox__selector-empty" v-if="filterCandidatesLength === 0">
						<text>{{emptyTips}}</text>
					</view>
					<view class="uni-combox__selector-item" v-for="(item,index) in filterCandidates" :key="item.id" @click="onSelectorClick(item.id)">
						<text>{{item.label || item}}</text>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	let tempData;
	export default {
		name: 'uniCombox',
		components: {
			
		},
		props: {
			label: {
				type: String,
				default: ''
			},
			labelWidth: {
				type: String,
				default: 'auto'
			},
			placeholder: {
				type: String,
				default: ''
			},
			candidates: {
				type: Array,
				default () {
					return []
				}
			},
			emptyTips: {
				type: String,
				default: '无匹配项'
			},
			value: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				showSelector: false,
				inputVal: ''
			}
		},
		computed: {
			labelStyle() {
				if (this.labelWidth === 'auto') {
					return {}
				}
				return {
					width: this.labelWidth
				}
			},
			filterCandidates() {
				return this.candidates.filter((item) => {
					let temp = item.label || item
					return temp.indexOf(this.inputVal) > -1
				})
			},
			filterCandidatesLength() {
				return this.filterCandidates.length
			}
		},
		watch: {
			value: {
				handler(newVal) {
					console.log('进入到这来')
					this.inputVal = newVal
				},
				immediate: true
			}
		},
		methods: {
			onFocus() {
				this.showSelector = true
			},
			onBlur() {
				setTimeout(() => {
					// this.showSelector = false
				},50)
			},
			onMaskTap() {
				/* IOS不能失去焦点 */
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord()
				// #endif
				this.showSelector = false
			},
			onSelectorClick(index) {
				/* IOS不能失去焦点 */
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord()
				// #endif
				// this.inputVal = this.filterCandidates[index].label || this.filterCandidates[index]
				
				tempData = this.filterCandidates.filter(function(item){
					return item.id == index
				})
				this.inputVal = tempData[0].label
				this.showSelector = false
				this.$emit('input', tempData[0])
			},
			onInput() {
				setTimeout(() => {
					// this.$emit('input', this.inputVal)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.uni-combox {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		height: 40px;
		flex-direction: row;
		align-items: center;
		// border-bottom: solid 1px #DDDDDD;
	}

	.uni-combox__label {
		font-size: 16px;
		line-height: 22px;
		padding-right: 10px;
		color: #999999;
	}

	.uni-combox__input-box {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex: 1;
		flex-direction: row;
		align-items: center;
	}

	.uni-combox__input {
		flex: 1;
		font-size: 26rpx;
		height: 22px;
		line-height: 22px;
	}

	.uni-combox__input-arrow {
		padding: 10px;
	}

	.uni-combox__selector {
		box-sizing: border-box;
		position: absolute;
		top: 30px;
		left: 0;
		width: 100%;
		background-color: #FFFFFF;
		border-radius: 6px;
		box-shadow: #DDDDDD 4px 4px 8px, #DDDDDD -4px -4px 8px;
		z-index: 2;
	}
	.uni-combox-mask{
		width: 100%;
		height: 100%;
		// background: rgba(0, 0, 0, 0.8);
		position: fixed;
		left: 0;
		top: 0;
		bottom: 0;
		right: 0;
		z-index: 1;
	}
	.uni-combox__selector-scroll {
		max-height: 200px;
		box-sizing: border-box;
	}

	.uni-combox__selector::before {
		content: '';
		position: absolute;
		width: 0;
		height: 0;
		border-bottom: solid 6px #FFFFFF;
		border-right: solid 6px transparent;
		border-left: solid 6px transparent;
		left: 10%;
		top: -6px;
		margin-left: -6px;
	}

	.uni-combox__selector-empty,
	.uni-combox__selector-item {
		/* #ifdef APP-NVUE */
		display: flex;
		/* #endif */
		line-height: 36px;
		font-size: 14px;
		padding-left: 30rpx;
		text-align: left;
		border-bottom: solid 1px #DDDDDD;
		margin: 0px 10px;
	}

	.uni-combox__selector-empty:last-child,
	.uni-combox__selector-item:last-child {
		border-bottom: none;
	}
</style>
